package com.ys.util.file;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * 读取文件信息-工具类
 */
@Component
public class ReadFileUtil {

    /**
     * 读取本地文件信息
     * @param filePath 文件路径
     * @return 文件文本内容
     * @throws Exception
     */
    public static String readFileInfo(String filePath) throws Exception{
        File file = new File(filePath);
        FileInputStream fileInputStream = new FileInputStream(file);

        // 根据文件类型调用适当的处理方法
        switch (fileTypeName(filePath)) {
            case "doc":
                return readFileFromDOC(fileInputStream);
            case "docx":
                return readFileFromDOCX(fileInputStream);
            case "pdf":
                return readFileFromPDF(fileInputStream);
            case "txt":
                return readFileFromTXT(fileInputStream);
            default:
                throw new RuntimeException("不支持的文件格式，只支持(DOC/DOCX/PDF/TXT)");
        }

    }

    /**
     * 读取本地文件信息
     * @param inputStream
     * @return 文件名
     * @throws Exception
     */
    public static String readFileInfoByInputStream(InputStream inputStream,String fileName) throws Exception{
        // 根据文件类型调用适当的处理方法
        switch (fileTypeName(fileName)) {
            case "doc":
                return readFileFromDOC(inputStream);
            case "docx":
                return readFileFromDOCX(inputStream);
            case "pdf":
                return readFileFromPDF(inputStream);
            case "txt":
                return readFileFromTXT(inputStream);
            default:
                throw new RuntimeException("不支持的文件格式，只支持(DOC/DOCX/PDF/TXT)");
        }

    }


    /***
     * 获取文件后缀
     * @param filePath
     * @return
     */
    private static String fileTypeName(String filePath) {
        int dotIndex = filePath.lastIndexOf(".");
        if (dotIndex > 0) {
            return filePath.substring(dotIndex + 1).toLowerCase();
        }
        return "";
    }

    /**
     * 读取docx文件信息
     */
    public static String readFileFromDOCX(InputStream inputStream) throws Exception{
        XWPFDocument document = new XWPFDocument(inputStream);
        XWPFWordExtractor extractor = new XWPFWordExtractor(document);
        String content = extractor.getText();
        // 关闭资源
        extractor.close();
        document.close();
        return content;
    }

    /**
     * 读取doc文件信息
     */
    public static String readFileFromDOC(InputStream inputStream) throws Exception{
        HWPFDocument document = new HWPFDocument(inputStream);
        WordExtractor extractor = new WordExtractor(document);
        String content = extractor.getText();
        // 关闭资源
        extractor.close();
        document.close();
        return content;
    }

    /**
     * 读取pdf文件信息
     */
    public static String readFileFromPDF(InputStream inputStream) throws Exception{
        PDDocument pdfDocument = PDDocument.load(inputStream);
        PDFTextStripper textStripper = new PDFTextStripper();
        String content = textStripper.getText(pdfDocument);
        pdfDocument.close();
        return content;
    }

    /**
     * 读取txt文件信息
     */
    public static String readFileFromTXT(InputStream inputStream) throws Exception{
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf8"));
        String content = "";
        String line;
        while ((line = reader.readLine()) != null) {
            content +="\n"+line;
        }
        reader.close();
        return content;
    }



    //开发测试 C:\Users\Administrator\Desktop\ai相关\不同格式知识库
    public static void main(String[] args){
        try {
            String input = readFileInfo("C:\\Users\\Administrator\\Desktop\\ai相关\\不同格式知识库\\111.doc");
            //System.out.println(readFileInfo("C:\\Users\\Administrator\\Desktop\\ai相关\\不同格式知识库\\111.txt"));
            //String input = "这里\n\n\n\n有多\n个\n连\t\t\t\t\t\t\t续\n的\n换行";
            System.out.println(input);
            String output = input.trim();
            System.out.println(output);
            String output2 = output.replaceAll("\\s+", "\n"); // 将连续空白字符（包括换行）替换为单个换行
            System.out.println("**********");
            System.out.println(output2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
